<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <th:block th:include="common/common_head :: commonHeader('供应商配额录入')"/>
    <link th:href="@{/static/css/plugins/cropper/cropper.min.css}" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <form class="form-horizontal" method="post" id="quotaForm">
        <div class="row">
            <div class="form-group">
                <div class="col-sm-8" style="margin-left: 16px">
                    <input class="btn btn-primary" type="button" onclick="save()" value="保存">
                    <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
                </div>
            </div>
            <!-- 基本信息 -->
            <div id="basicinformation">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="col-xs-1 control-label"><span class="required-sign">*</span>供应商编码：</label>
                            <div class="col-xs-3">
                                <input id="vendorCode" name="vendorCode" type="text" class="form-control" required>
                            </div>
                            <label class="col-xs-1 control-label">供应商名称：</label>
                            <div class="col-xs-3">
                                <input class="form-control" id="vendorName" name="vendorName" readonly>
                            </div>
                            <label class="col-xs-1 control-label"><span class="required-sign">*</span>工厂：</label>
                            <div class="col-xs-3">
                                <select class="form-control" id="werks" name="werks" sysDict="GC" emptyFirst="false"
                                        required></select>
                            </div>

                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label"><span class="required-sign">*</span>物料号：</label>
                            <div class="col-sm-3">
                                <input class="form-control required" id="materialCode" name="materialCode" required>
                            </div>
                            <label class="col-sm-1 control-label">物料描述：</label>
                            <div class="col-sm-3">
                                <input class="form-control" id="materialDesc" name="materialDesc" type="text" readonly>
                            </div>
                            <label class="col-xs-1 control-label"><span class="required-sign">*</span>配额数：</label>
                            <div class="col-xs-3">
                                <input name="quotaNum" type="number" class="form-control" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label"><span class="required-sign">*</span>开始时间：</label>
                            <div class="col-xs-3">
                                <input id="startDate" name="startDate" type="text" class="form-control" required
                                       autocomplete="off">
                            </div>
                            <label class="col-sm-1 control-label"><span class="required-sign">*</span>结束时间：</label>
                            <div class="col-xs-3">
                                <input id="endDate" name="endDate" type="text" class="form-control" autocomplete="off"
                                       required>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<!-- Chosen -->
<script th:src="@{/static/js/plugins/chosen/chosen.jquery.js}"></script>
<script type="text/javascript">

    $(function () {
        //初始化所有字典数据
        initDictDataSel();
        // 初始化时间插件
        laydate.render({elem: '#startDate'});
        laydate.render({elem: '#endDate'});
    });

    //保存
    function save() {
        if ($("#quotaForm").valid()) {

            if ($('#startDate').val() == null || $('#startDate').val() == undefined || $('#startDate').val() == '') {
                Dialog.errorMsg("请选择开始时间!");
                return;
            }
            if ($('#endDate').val() == null || $('#endDate').val() == undefined || $('#endDate').val() == '') {
                Dialog.errorMsg("请选择结束时间!");
                return;
            }
            if ($('#endDate').val() < $('#startDate').val()) {
                Dialog.errorMsg("开始时间不能大于结束时间！");
                return;
            }
            if (new Date($('#endDate').val()) < new Date()) {
                Dialog.errorMsg("结束时间不能小于今天！");
                return;
            }
            //先根据vendorCode、materialCode查询是否存在
            layer.load(1, {shade: [0.3, '#393D49']});
            $.ajax({
                url: ctxPath + "/quota/checkExist",
                data: {
                    "vendorCode": $("#vendorCode").val().trim(),
                    "materialCode": $("#materialCode").val().trim(),
                    "werks": $("#werks").val().trim(),
                    "startDate": $('#startDate').val()
                },
                type: 'POST',
                dataType: 'JSON',
                success: function (data) {
                    if (data.code == 0) {
                        Page.ajaxPOST(ctxPath + "/quota/save", $('#quotaForm').serialize(), function (res) {
                            layer.closeAll();
                            Dialog.msg("新增成功!", function () {
                                parent.refresh();
                                Dialog.closeFrame(window.name);
                            });
                        });
                    } else {
                        layer.closeAll();
                        Dialog.errorMsg(data.msg, null, {time: 3000});
                    }
                }
            });
        }
    }

    //根据vendorCode查vendorName
    $("#vendorCode").change(function () {
        let vendorCode = $(this).val();
        $.ajax({
            url: ctxPath + "/vendor/findById/" + vendorCode,
            type: 'POST',
            dataType: 'JSON',
            success: function (data) {
                if (data.result) {
                    $("#vendorName").val(data.result.vendorName);
                } else {
                    Dialog.errorMsg("不存在该供应商，请确认!");
                }
            }
        });
    });

    //根据materialCode查materialDesc
    $("#materialCode").change(function () {
        let materialCode = $(this).val();
        $.ajax({
            url: ctxPath + "/material/getMaterialByCode",
            data: {"materialCode": materialCode},
            dataType: 'JSON',
            success: function (data) {
                if (data.result) {
                    $("#materialDesc").val(data.result.materialDesc);
                } else {
                    Dialog.errorMsg("不存在该物料，请确认!");
                }
            }
        });
    });

</script>

</body>
</html>
